package com.tuanzi.loan.web.bean.family;

import com.base.common.log.LogService;
import com.tuanzi.loan.business.service.family.LoanWholeSaleCommentService;
import com.tuanzi.loan.vo.request.family.LoanWholeSaleCommentSearchRequest;
import com.tuanzi.loan.vo.response.family.LoanWholeSaleCommentSearchResponse;
import com.tuanzi.loan.vo.view.family.CommRegionNewInfo;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.family.LoanWholeSaleCommentLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanWholeSaleCommentBean extends JSFListingBean<LoanWholeSaleCommentSearchRequest, LoanWholeSaleCommentSearchResponse> {
    @Autowired
    LoanWholeSaleCommentService loanWholeSaleCommentService;
    private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    @PostConstruct
    private void init() {
        initLazyDataModel();
    }

    public void onReset() {
        initLazyDataModel();
    }

    public void onRefresh(){
        lazyDataModel = new LoanWholeSaleCommentLazyDataModel($);
        addSuccessfullyMessage();
    }

    private void initLazyDataModel() {
        $ = new LoanWholeSaleCommentSearchRequest();
        lazyDataModel = new LoanWholeSaleCommentLazyDataModel($);
    }


    public void OnDown(LoanWholeSaleCommentSearchResponse response){
        if (response.isEnable()){
            try {
                if (loanWholeSaleCommentService.updateStatus((Long) response.getId())>0)
                addSuccessfullyMessage();
            } catch (Exception e) {
                addErrorMessage("下线失败\n"+e.getMessage());
            }
        }
    }

    public void OnUpper(LoanWholeSaleCommentSearchResponse response){
        if (response.isDisable()){
            try {
                if (loanWholeSaleCommentService.updateStatus((Long) response.getId())>0)
                    addSuccessfullyMessage();
            } catch (Exception e) {
                addErrorMessage("上线失败\n"+e.getMessage());
            }
        }
    }

    public void OnDelete(LoanWholeSaleCommentSearchResponse response){
        try {
            loanWholeSaleCommentService.OnDelete(response);
            addSuccessfullyMessage();
        } catch (Exception e) {
            addErrorMessage("删除失败");
            LogService.error("删除失败",e);
        }
    }

    public String showDate(Date date){
        return sdf.format(date);
    }

    public String translateStatus(String status){
        return "1".equals(status.trim())?"上线中":"已下线";
    }

    public List<CommRegionNewInfo> findAllRegionInfo(){
        return loanWholeSaleCommentService.findAllRegionInfo();
    }
    public String add(){
        return redirect("/views/family/WholeSaleManage/WholeSaleManageInfo.xhtml");
    }

    public String edit(String id){
        return redirect("/views/family/WholeSaleManage/WholeSaleManageInfo.xhtml?id="+id);
    }
}
